package com.example.myapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class zzzzz2025111001MainActivity extends AppCompatActivity {
    private EditText tv_Vc;
    private TextView tv_Sms;
    private Button btnSms;
    private String text = "";
    private static final int SMS_PERMISSION_CODE = 101;
    private static final String TARGET_NUMBER = "01095588"; // 目标发送号码
    private static final String CODE_PATTERN = "\\d{6}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.zzzzz2025111001activity_main);
        init();
    }
    
    private void init() {
        tv_Sms = findViewById(R.id.zzzzz2025111001001_tv_sms);
        tv_Vc = findViewById(R.id.zzzzz2025111001001_tv_VC);
        btnSms = findViewById(R.id.zzzzz2025111001001_btn_sms);
        btnSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkPermission()) {
                    getSms();
                } else {
                    requestPermission();
                }
            }
        });
    }

    private boolean checkPermission() {
        return ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) 
                == PackageManager.PERMISSION_GRANTED;
    }

    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(zzzzz2025111001MainActivity.this,
                    new String[]{Manifest.permission.READ_SMS}, SMS_PERMISSION_CODE);
        }
    }

    public void getSms() {
        Cursor cursor = null;
        try {
            Uri uri = Uri.parse("content://sms/inbox");
            String[] projection = {"address", "body", "date"};
            String selection = "address=? and body like ?";
            String[] selectionArgs = {TARGET_NUMBER, "%验证码%"};
            String sortOrder = "date desc limit 5";
            cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
            if (cursor != null && cursor.moveToFirst()) {
                String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
                extractVerificationCode(body);
                do {
                    text += "短信号码：" + cursor.getString(0) + "\n";
                    text += "短信内容：" + cursor.getString(1) + "\n\n";
                    tv_Sms.setText(text);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    private void extractVerificationCode(String smsBody) {
        Pattern pattern = Pattern.compile(CODE_PATTERN);
        Matcher matcher = pattern.matcher(smsBody);
        if (matcher.find()) {
            String code = matcher.group();
            tv_Vc.setText(code);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == SMS_PERMISSION_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getSms();
            } else {
                Toast.makeText(this, "权限被拒绝，无法读取短信", Toast.LENGTH_SHORT).show();
            }
        }
    }
}